package com.example.administrator.newsapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;


public class LoginActivity extends AppCompatActivity {

    private EditText etPhone;
    private EditText etCode;
    private Button btnSentCode;
    private Button btnLogin;
    private String strPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        btnSentCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                strPhone = etPhone.getText().toString();
                if (!strPhone.equals("")) {
                    if (isValid(strPhone)) {
                        BmobSMS.requestSMSCode(strPhone, "验证码", new QueryListener<Integer>() {
                            @Override
                            public void done(Integer smsId, BmobException e) {
                                if (e == null) {
                                    Toast.makeText(LoginActivity.this, "发送短信成功，短信id：" + smsId, Toast.LENGTH_SHORT).show();
                                    smsCodeByPhone(strPhone);
                                } else {
                                    Toast.makeText(LoginActivity.this, "本app短信余额已用完，请联系管理员" + smsId, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    } else {
                        Toast.makeText(LoginActivity.this, "您输入的手机号码无效", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "请先输入手机号", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        etPhone = findViewById(R.id.et_phone);
        etCode = findViewById(R.id.et_code);
        btnSentCode = findViewById(R.id.btn_sentCode);
        btnLogin = findViewById(R.id.btn_login);
    }

    private void smsCodeByPhone(final String strPhone) {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String smscode = etCode.getText().toString();
                if (!smscode.equals("")) {
                    BmobUser.signOrLoginByMobilePhone(strPhone, smscode, new LogInListener<Object>() {
                        @Override
                        public void done(Object o, BmobException e) {
                            Toast.makeText(LoginActivity.this, "用户登录成功", Toast.LENGTH_SHORT).show();
                            BmobUser bmobUser = BmobUser.getCurrentUser();
                            Intent intent = getIntent();
                            intent.putExtra("BMOBUSER",bmobUser);
                            setResult(MainActivity.LOGIN_SUC, intent);
                            finish();
                        }
                    });
                }
            }
        });
    }


    private boolean isValid(String phone) {
        String regex = "^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }
}
